package com.woniu.controller;

import com.woniu.common.CommonResult;
import com.woniu.common.CookieUtil;
import com.woniu.entity.PersonImage;
import com.woniu.entity.PersonUser;
import com.woniu.service.PersonImageService;
import com.woniu.service.PersonUserService;
import com.woniu.service.UsedProductCommentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * (PersonUser)表控制层
 *
 * @author makejava
 * @since 2020-03-24 19:04:31
 */
@RestController
@RequestMapping("/personUser")
public class PersonUserController {
    /**
     * 服务对象
     */
    @Resource
    private PersonUserService personUserService;

    @Resource
    PersonImageService personImageService;

    @Resource
    UsedProductCommentService usedProductCommentService;

    @Resource
    CookieUtil cookieUtil;

    /**
     * 功能描述: 根据商品Id查询当前商品评论用户的所有信息
     * @Param: []
     * @Return: com.woniu.common.CommonResult
     * @Author: yanghan
     * @Date: 2020/4/3 2:32
     */
    @GetMapping("/findByUserId/{productId}")
    public  CommonResult findByUserId(@PathVariable("productId")Integer productId){
        try {
            Integer userId = usedProductCommentService.findUserIdByProductId(productId);
            //根据userId查询其所有信息，以及头像图片
            PersonUser personUser = personUserService.queryById(userId);
            String url = personImageService.findAllByUserId(userId);
            Map<String, Object> map = new HashMap<>();
            map.put("user",personUser);
            map.put("url",url);
            return CommonResult.success("查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("查询失败");
        }


    }

    /**
     * 功能描述: 查询当前用户所属小区的所有人的昵称以及头像url
     * @Param: []
     * @Return: com.woniu.common.CommonResult
     * @Author: yanghan
     * @Date: 2020/3/30 2:53
     */
    @GetMapping("/peopleNearby")
    public CommonResult peopleNearby(){
        //当前登录用户名
//        String name = cookieUtil.getName();
        //查询当前用户所属小区
        String name = "admin";
        //当前用户信息
        PersonUser personUser = personUserService.findIdByName(name);
        Integer plotId = personUserService.findPlotIdByUserId(personUser.getPlotId());
        //查询小区中的最后三个人
        List<PersonUser> personUsers = personUserService.findPersonByPoltId(plotId);
        ArrayList<Integer> userId = new ArrayList<>();
        ArrayList<String> nickNames = new ArrayList<>();
        for (PersonUser person:personUsers) {
            userId.add(person.getId());
            nickNames.add(person.getNickname());
        }
        //查询这三个人的头像
        List<PersonImage> personImages = personImageService.findImgByListUserId(userId);
        ArrayList<String> images = new ArrayList<>();
        for (PersonImage pImage:personImages) {
            images.add(pImage.getUrl());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("nickNames",nickNames);
        map.put("images",images);
        return CommonResult.success(map);
    }

    /**
     * 功能描述: 根据用户Id查询用户昵称
     * @Param: []
     * @Return: com.woniu.common.CommonResult
     * @Author: yanghan
     * @Date: 2020/3/30 2:53
     */
    @GetMapping("/findCurrentUserName")
    public CommonResult findCurrentUserName(){
        //当前登录用户名
//        String name = cookieUtil.getName();
//        if (name == null){
//            return CommonResult.success("未登录");
//        }
        String name = "admin";
        //根据用户名查询用户昵称
        try {
            String nickName = personUserService.findNickNameByName(name);
            return CommonResult.success(nickName);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }


    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public PersonUser selectOne(Integer id) {
        return this.personUserService.queryById(id);
    }

}